package com.service.seedoctor.impl;

import com.comm.R;
import com.comm.vo.DoctorShiftsParams;
import com.entity.DoctorShifts;
import com.entity.Employee;
import com.entity.RegistrationFee;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mapper.seedoctor.DoctorShiftMapper;
import com.mapper.setting.SysEmployeeMapper;
import com.service.seedoctor.DoctorShiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class DoctorShiftServiceImpl implements DoctorShiftService {

    @Autowired
    private SysEmployeeMapper sysEmployeeMapper;

    @Autowired
    private DoctorShiftMapper shiftMapper;
    @Override
    public R returnAllDoctorInfo(String currentPage,String pageSize) {
        R r=R.ok();
        r.setCount(sysEmployeeMapper.getDoctorCount());
        Page<Employee> employees=PageHelper.startPage(Integer.valueOf(currentPage),Integer.valueOf(pageSize));
        List<Employee> allDoctorInfo = sysEmployeeMapper.getAllDoctorInfo();
        System.out.println(allDoctorInfo);
        for (Employee e:allDoctorInfo) {
            e.setOffice(sysEmployeeMapper.getOfficeById(e.getOfficeId()));
        }
        return r.setData(allDoctorInfo);

    }

    @Override
    public R getAllDoctorsShiftInfoInAWeek(DoctorShiftsParams params) {

        Integer currentPage = params.getCurrentPage();
        String startDate = params.getStartDate();
        String endDate = params.getEndDate();
        List<Integer> doctorIds = params.getDoctorIds();
        /*Page<DoctorShifts> page =PageHelper.startPage(currentPage,1000 );*/
        List<DoctorShifts> allDoctorInfo =shiftMapper.getAllDoctorShiftInfoInAWeek(startDate,endDate,doctorIds);
        Iterator<DoctorShifts> iterator = allDoctorInfo.iterator();
        if (iterator.hasNext()){
            DoctorShifts next = iterator.next();
            next.setRegistrationFee(shiftMapper.getRegistrationById(next.getRegTypeId()));
            next.setOffice(sysEmployeeMapper.getOfficeById(next.getDepartmentId().intValue()));
        }
        return R.ok(allDoctorInfo).setMsg("成功！");
    }

    @Override
    public R getOneDoctorShilfInfo(DoctorShiftsParams params) {
        Integer currentPage = params.getCurrentPage();
        String startDate = params.getStartDate();
        String endDate = params.getEndDate();
        Integer doctorId = params.getDoctorId();
        /*Page<DoctorShifts> page =PageHelper.startPage(currentPage,5 );*/
        List<DoctorShifts> allDoctorInfo =shiftMapper.getOneDoctorShilfInfo(startDate,endDate,doctorId);
        Iterator<DoctorShifts> iterator = allDoctorInfo.iterator();
        if (iterator.hasNext()){
            DoctorShifts next = iterator.next();
            next.setRegistrationFee(shiftMapper.getRegistrationById(next.getRegTypeId()));
            next.setRegistrationFee(shiftMapper.getRegistrationById(next.getRegTypeId()));
        }
        return R.ok(allDoctorInfo).setMsg("成功！");
    }

    @Override
    public R getRegistrationFee() {

        List<RegistrationFee> registrationFee = shiftMapper.getRegistrationFee();
        return  R.ok(registrationFee).setMsg("成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateDoctorShifts(List<DoctorShifts> shiftsList) {

        final List<Long> ids=new ArrayList<>();
        Iterator<DoctorShifts> iterator = shiftsList.iterator();
        if (iterator.hasNext()){
            ids.add(iterator.next().getId());
        }
        shiftMapper.deleteDoctorShiftsByDoctorIds(ids);
        for (int i = 0; i < shiftsList.size(); i++) {
            shiftMapper.insertInDoctorShifts(shiftsList.get(i));
        }

        return R.ok().setMsg("编辑成功！");
    }
}
